package com.example.workrecord.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.workrecord.common.Page;
import com.example.workrecord.config.WebLog;
import com.example.workrecord.entity.CorpUserDetail;
import com.example.workrecord.entity.PlatForm;
import com.example.workrecord.exption.BizException;
import com.example.workrecord.service.PlatFormService;
import com.example.workrecord.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName PlatformController
 * @Description PlatformController
 * @Author Bu
 * @Date 2019/06/18 10:45
 * @Version 1.0
 */
@RestController
@Api(value = "platform", description = "平台成果管理端接口", consumes = "application/json;charset=UTF-8")
@RequestMapping(value = "/platform")
public class PlatformController extends BaseController {

	@Autowired
	private PlatFormService formService;


	@PostMapping("/save")
	@ResponseBody
	@WebLog(description = "新增Platform")
	@ApiOperation(value = "新增Platform", notes = "{\"log_url\":\"log图\",\"name\":\"平台名称\",\"short_name\":\"平台简称\",\"units\":\"所在二级单位\",\"research_field\":\"研究领域\",\"industry\":\"行业\",\"link_name\":\"联系人名称\",\"link_mob\":\"联系手机号\",\"link_email\":\"联系邮箱\",\"introduce\":\"平台介绍\",\"research_data\":\"科研数据\",\"results\":\"成果展示数组\"}")
	public Result save(HttpServletRequest request, @RequestBody PlatForm form) {
		try {
			CorpUserDetail user = getUserInfo(request);
			if(null == user || user.getUserid().equals("")){
				return Result.error("用户信息未找到，请重新登陆");
			}
			form.setCreateUser(user.getName());
			formService.savePlatform(form);
		}catch (Exception e){
			return Result.error(e.toString());
		}
		return Result.success(form);
	}


	@PostMapping("/delete")
	@ResponseBody
	@WebLog(description = "根据id删除Platform")
	@ApiOperation(value = "根据id删除Platform", notes = "{\\\"id\\\":\\\"id\\\"}")
	public Result delete(HttpServletRequest request,@RequestBody JSONObject res) {
		try {
			CorpUserDetail user = getUserInfo(request);
			if(null == user || user.getUserid().equals("")){
				return Result.error("用户信息未找到，请重新登陆");
			}
			checkNull(res,"参数不可为空");
			Integer id=res.getInteger("id");
			formService.deletePlatform(id);
		}catch (Exception e){
			return Result.error(e.toString());
		}
		return Result.success(null);
	}

	@PostMapping("/edit")
	@ResponseBody
	@WebLog(description = "修改Platform")
	@ApiOperation(value = "修改Platform", notes = "{\"id\":\"id\",\"log_url\":\"log图\",\"name\":\"平台名称\",\"short_name\":\"平台简称\",\"units\":\"所在二级单位\",\"research_field\":\"研究领域\",\"industry\":\"行业\",\"link_name\":\"联系人名称\",\"link_mob\":\"联系手机号\",\"link_email\":\"联系邮箱\",\"introduce\":\"平台介绍\",\"research_data\":\"科研数据\",\"results\":\"成果展示数组\"}")
	public Result edit(HttpServletRequest request, @RequestBody PlatForm form) {
		try {
			CorpUserDetail user = getUserInfo(request);
			if(null == user || user.getUserid().equals("")){
				return Result.error("用户信息未找到，请重新登陆");
			}
			formService.eidtPlatform(form);
		}catch (Exception e){
			return Result.error(e.toString());
		}
		return Result.success(form);
	}

	@PostMapping("/list")
	@ResponseBody
	@WebLog(description = "分页查询Platform")
	@ApiOperation(value = "分页查询Platform，根据条件查询", notes = "{\"userId\": \"用户id\",\"limit\": \"分页大小，此参数代表分页大小，最大100\",\"offset\": \"分页偏移，默认值：0\",\"startDate\": \"开始时间\",\"endDate\": \"结束时间\",\"searchDate\": \"搜索\"}")
	public Result findUserByData(HttpServletRequest request, @RequestBody PlatForm vo ){
		Page<PlatForm> page =new Page<>();
		try{
			CorpUserDetail user = getUserInfo(request);
			if(null ==user ){
				return Result.error("用户不可为空，请检查！");
			}
			checkNull(vo,"参数不可为空！");
			page.setPageCurrent(vo.getOffset()+1);
			page.setPageSize(vo.getLimit());
			formService.findUserByData(vo,page);
		}catch (BizException e){
			return Result.error(e.getMessage());
		}
		return Result.success(page);
	}

	@PostMapping("/findById")
	@ResponseBody
	@WebLog(description = "根据Id查询Platform")
	@ApiOperation(value = "根据Id查询Platform", notes = "{\"id\":\"id\"}")
	public Result findById(HttpServletRequest request, @RequestBody JSONObject res ){
		PlatForm form=new PlatForm();
		try{
			CorpUserDetail user = getUserInfo(request);
			if(null ==user ){
				return Result.error("用户不可为空，请检查！");
			}
			checkNull(res,"参数不可为空");
			Integer id=res.getInteger("id");
			form.setId(Long.valueOf(id));
			form=formService.findPlatFormById(form);
		}catch (BizException e){
			return Result.error(e.getMessage());
		}
		return Result.success(form);
	}


	@PostMapping("/deleteAll")
	@ResponseBody
	@WebLog(description = "根据id数组删除Platform")
	@ApiOperation(value = "根据id数组删除Platform", notes = "{\\\"ids\\\":\\\"id逗号分割\\\"}")
	public Result deleteAll(HttpServletRequest request,@RequestBody JSONObject res) {
		try {
			CorpUserDetail user = getUserInfo(request);
			if(null == user || user.getUserid().equals("")){
				return Result.error("用户信息未找到，请重新登陆");
			}
			checkNull(res,"参数不可为空");
			formService.deleteAllByIds(res);
		}catch (Exception e){
			return Result.error(e.toString());
		}
		return Result.success(null);
	}

	@PostMapping("/updateStatus")
	@ResponseBody
	@WebLog(description = "根据id数组删除Platform")
	@ApiOperation(value = "根据id数组删除Platform", notes = "{\"ids\":\"id逗号分割\",\"status\":\"0-上线，1-下线\"}")
	public Result updateStatus(HttpServletRequest request,@RequestBody JSONObject res) {
		try {
			CorpUserDetail user = getUserInfo(request);
			if(null == user || user.getUserid().equals("")){
				return Result.error("用户信息未找到，请重新登陆");
			}
			checkNull(res,"参数不可为空");
			formService.updateStatus(res);
		}catch (Exception e){
			return Result.error(e.toString());
		}
		return Result.success(null);
	}


	@PostMapping("/updateOrder")
	@ResponseBody
	@WebLog(description = "根据id数组删除Platform")
	@ApiOperation(value = "根据id数组删除Platform", notes = "{\"ids\":\"id逗号分割\",\"hOrder\":\"排序字段\"}")
	public Result hOrder(HttpServletRequest request,@RequestBody List<PlatForm> list) {
		try {
			CorpUserDetail user = getUserInfo(request);
			if(null == user || user.getUserid().equals("")){
				return Result.error("用户信息未找到，请重新登陆");
			}
			formService.hOrder(list);
		}catch (Exception e){
			return Result.error(e.toString());
		}
		return Result.success(null);
	}


}
